﻿using QFramework;
using UnityEngine;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/GameDataManager")]
    public class GameDataManager : MonoBehaviour, ISingleton
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
    
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static GameDataManager Instance
        {
            get { return MonoSingletonProperty<GameDataManager>.Instance; }
        }

        /// <summary>
        /// 保存Int数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SetInt(string key, int value)
        {
            PlayerPrefs.SetInt(key, value);
        }

        /// <summary>
        /// 获取Int数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public int GetInt(string key, int DefaultValue = 0)
        {
            return PlayerPrefs.GetInt(key, DefaultValue);
        }

        /// <summary>
        /// 保存float数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SetFloat(string key, float value)
        {
            PlayerPrefs.SetFloat(key, value);
        }

        /// <summary>
        /// 获取float数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public float GetFloat(string key, float DefaultValue = 0.0f)
        {
            return PlayerPrefs.GetFloat(key, DefaultValue);
        }

        /// <summary>
        /// 保存string数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SetString(string key, string value)
        {
            PlayerPrefs.SetString(key, value);
        }

        /// <summary>
        /// 获取string数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public string GetString(string key, string DefaultValue = "")
        {
            return PlayerPrefs.GetString(key, DefaultValue);
        }

        /// <summary>
        /// 保存bool数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SetBool(string key, bool value)
        {
            PlayerPrefs.SetInt(key, value ? 1 : 0);
        }

        /// <summary>
        /// 获取bool数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public bool GetBool(string key, bool DefaultValue = false)
        {
            return PlayerPrefs.GetInt(key, DefaultValue ? 1 : 0) == 1;
        }

        /// <summary>
        /// 保存double数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SetDouble(string key, double value)
        {
            PlayerPrefs.SetString(key, value.ToString());
        }

        /// <summary>
        /// 获取double数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public double GetDouble(string key, double DefaultValue = 0.0d)
        {
            string value = PlayerPrefs.GetString(key, DefaultValue.ToString());
            return double.Parse(value);
        }

        /// <summary>
        /// 保存类数据
        /// </summary>
        /// <typeparam name="T">可序列化的类</typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SetObject<T>(string key, T value)
        {
            string json = JsonUtility.ToJson(value);
            PlayerPrefs.SetString(key, json);
        }

        /// <summary>
        /// 获取类数据
        /// </summary>
        /// <typeparam name="T">可序列化的类</typeparam>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public T GetObject<T>(string key, T DefaultValue)
        {
            string json = PlayerPrefs.GetString(key, null);
            if (string.IsNullOrEmpty(json))
            {
                return DefaultValue;
            }
            return JsonUtility.FromJson<T>(json);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="key">键</param>
        public void DeleteKey(string key)
        {
            if (PlayerPrefs.HasKey(key))
            {
                PlayerPrefs.DeleteKey(key);
            }
            else
            {
                LogKit.E("不存在数据:" + key + ",删除失败");
            }
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void DeleteAll()
        {
            PlayerPrefs.DeleteAll();
        }

        /// <summary>
        /// 是否存在key
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public bool HasKey(string key)
        {
            return PlayerPrefs.HasKey(key);
        }
    }
}